home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 5 / Example 5.2 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  6.8 KB  |  257 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 5.2: Mouse Example                                //
  3. // Written by: C. Granberg, 2006                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include <vector>
  9. #include "debug.h"
  10. #include "mouse.h"
  11.  
  12. class APPLICATION
  13. {
  14.     public:
  15.         APPLICATION();
  16.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  17.         HRESULT Update(float deltaTime);
  18.         HRESULT Render();
  19.         HRESULT Cleanup();
  20.         HRESULT Quit();
  21.  
  22.     private:
  23.         IDirect3DDevice9* m_pDevice; 
  24.         MOUSE m_mouse;
  25.  
  26.         RECT m_rects[4];
  27.         HWND m_mainWindow;
  28.         ID3DXFont *m_pFont;
  29. };
  30.  
  31. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  32. {
  33.     APPLICATION app;
  34.  
  35.     if(FAILED(app.Init(hInstance, 800, 600, true)))
  36.         return 0;
  37.  
  38.     MSG msg;
  39.     memset(&msg, 0, sizeof(MSG));
  40.     int startTime = timeGetTime(); 
  41.  
  42.     while(msg.message != WM_QUIT)
  43.     {
  44.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  45.         {
  46.             ::TranslateMessage(&msg);
  47.             ::DispatchMessage(&msg);
  48.         }
  49.         else
  50.         {    
  51.             int t = timeGetTime();
  52.             float deltaTime = (t - startTime)*0.001f;
  53.  
  54.             app.Update(deltaTime);
  55.             app.Render();
  56.  
  57.             startTime = t;
  58.         }
  59.     }
  60.  
  61.     app.Cleanup();
  62.  
  63.     return msg.wParam;
  64. }
  65.  
  66. APPLICATION::APPLICATION()
  67. {
  68.     m_pDevice = NULL; 
  69.     m_mainWindow = 0;
  70.  
  71.     srand(GetTickCount());
  72. }
  73.  
  74. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  75. {
  76.     debug.Print("Application initiated");
  77.  
  78.     //Create Window Class
  79.     WNDCLASS wc;
  80.     memset(&wc, 0, sizeof(WNDCLASS));
  81.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  82.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  83.     wc.hInstance     = hInstance;
  84.     wc.lpszClassName = "D3DWND";
  85.  
  86.     //Register Class and Create new Window
  87.     RegisterClass(&wc);
  88.     m_mainWindow = CreateWindow("D3DWND", "Example 5.2: Mouse Example", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  89.     SetCursor(NULL);
  90.     ShowWindow(m_mainWindow, SW_SHOW);
  91.     UpdateWindow(m_mainWindow);
  92.  
  93.     //Create IDirect3D9 Interface
  94.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  95.  
  96.     if(d3d9 == NULL)
  97.     {
  98.         debug.Print("Direct3DCreate9() - FAILED");
  99.         return E_FAIL;
  100.     }
  101.  
  102.     //Check that the Device supports what we need from it
  103.     D3DCAPS9 caps;
  104.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  105.  
  106.     //Hardware Vertex Processing or not?
  107.     int vp = 0;
  108.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  109.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  110.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  111.  
  112.     //Check vertex & pixelshader versions
  113.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  114.     {
  115.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  116.     }
  117.  
  118.     //Set D3DPRESENT_PARAMETERS
  119.     D3DPRESENT_PARAMETERS d3dpp;
  120.     d3dpp.BackBufferWidth            = width;
  121.     d3dpp.BackBufferHeight           = height;
  122.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  123.     d3dpp.BackBufferCount            = 1;
  124.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  125.     d3dpp.MultiSampleQuality         = 0;
  126.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  127.     d3dpp.hDeviceWindow              = m_mainWindow;
  128.     d3dpp.Windowed                   = windowed;
  129.     d3dpp.EnableAutoDepthStencil     = true; 
  130.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  131.     d3dpp.Flags                      = 0;
  132.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  133.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  134.  
  135.     //Create the IDirect3DDevice9
  136.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  137.                                  vp, &d3dpp, &m_pDevice)))
  138.     {
  139.         debug.Print("Failed to create IDirect3DDevice9");
  140.         return E_FAIL;
  141.     }
  142.  
  143.     //Release IDirect3D9 interface
  144.     d3d9->Release();
  145.  
  146.     D3DXCreateFont(m_pDevice, 18, 0, 0, 1, false,  
  147.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  148.                    DEFAULT_PITCH | FF_DONTCARE, "Arial", &m_pFont);
  149.  
  150.     //Set sampler state
  151.     for(int i=0;i<4;i++)
  152.     {
  153.         m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  154.         m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  155.         m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  156.     }
  157.  
  158.     //Init mouse
  159.     m_mouse.InitMouse(m_pDevice, m_mainWindow);
  160.  
  161.     //Setup rectangles
  162.     RECT t1 = {200, 100, 350, 250};
  163.     m_rects[0] = t1;
  164.     RECT t2 = {450, 100, 600, 250};
  165.     m_rects[1] = t2;
  166.     RECT t3 = {200, 350, 350, 500};
  167.     m_rects[2] = t3;
  168.     RECT t4 = {450, 350, 600, 500};
  169.     m_rects[3] = t4;
  170.  
  171.     return S_OK;
  172. }
  173.  
  174. HRESULT APPLICATION::Update(float deltaTime)
  175. {
  176.     //Update mouse
  177.     m_mouse.Update();
  178.  
  179.     //Change mouse
  180.     if(m_mouse.WheelUp() && m_mouse.m_speed < 3.0f)m_mouse.m_speed += 0.1f;
  181.     if(m_mouse.WheelDown() && m_mouse.m_speed > 0.3f)m_mouse.m_speed -= 0.1f;
  182.  
  183.     if(m_mouse.PressInRect(m_rects[0]))m_mouse.m_type = 1;        //Red Square
  184.     if(m_mouse.PressInRect(m_rects[1]))m_mouse.m_type = 2;        //Green Square
  185.     if(m_mouse.PressInRect(m_rects[2]))m_mouse.m_type = 3;        //Blue Square
  186.     if(m_mouse.PressInRect(m_rects[3]))m_mouse.m_type = 4;        //Yellow Square
  187.  
  188.     if(KEYDOWN(VK_ESCAPE))
  189.         Quit();
  190.  
  191.     return S_OK;
  192. }    
  193.  
  194. HRESULT APPLICATION::Render()
  195. {
  196.     // Clear the viewport
  197.     m_pDevice->Clear(0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L );
  198.  
  199.     D3DXCOLOR rectCols[] = {D3DXCOLOR(0.5f, 0.0f, 0.0f, 1.0f), D3DXCOLOR(0.0f, 0.5, 0.0f, 1.0f), D3DXCOLOR(0.0f, 0.0f, 0.5f, 1.0f), D3DXCOLOR(0.5f, 0.5f, 0.0f, 1.0f)};
  200.     D3DXCOLOR rectColsOver[] = {D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f), D3DXCOLOR(0.0f, 1.0, 0.0f, 1.0f), D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f), D3DXCOLOR(1.0f, 1.0f, 0.0f, 1.0f)};
  201.  
  202.     //Draw colour rectangles
  203.     for(int i=0;i<4;i++)
  204.     {
  205.         D3DRECT r;
  206.         r.x1 = m_rects[i].left;
  207.         r.y1 = m_rects[i].top;
  208.         r.x2 = m_rects[i].right;
  209.         r.y2 = m_rects[i].bottom;
  210.  
  211.         if(m_mouse.Over(m_rects[i]))
  212.             m_pDevice->Clear(1, &r, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, rectColsOver[i], 1.0f, 0L );
  213.         else m_pDevice->Clear(1, &r, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, rectCols[i], 1.0f, 0L );
  214.     }
  215.  
  216.     
  217.     // Begin the scene 
  218.     if(SUCCEEDED(m_pDevice->BeginScene()))
  219.     {
  220.         char number[50];
  221.         std::string text = "Mouse Speed: ";
  222.         text += _itoa(m_mouse.m_speed * 10.0f, number, 10);
  223.         text += "     (Mouse Wheel)";
  224.         RECT r = {10, 10, 0, 0};
  225.         m_pFont->DrawText(NULL, text.c_str(), -1, &r, DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  226.  
  227.         //Draw mouse
  228.         m_mouse.Paint();
  229.  
  230.         // End the scene.
  231.         m_pDevice->EndScene();
  232.         m_pDevice->Present(0, 0, 0, 0);
  233.     }
  234.  
  235.     return S_OK;
  236. }
  237.  
  238. HRESULT APPLICATION::Cleanup()
  239. {
  240.     try
  241.     {
  242.         m_pFont->Release();
  243.         m_pDevice->Release();
  244.  
  245.         debug.Print("Application terminated");
  246.     }
  247.     catch(...){}
  248.  
  249.     return S_OK;
  250. }
  251.  
  252. HRESULT APPLICATION::Quit()
  253. {
  254.     ::DestroyWindow(m_mainWindow);
  255.     ::PostQuitMessage(0);
  256.     return S_OK;
  257. }